package com.rp.system.dao;

import java.util.List;

import org.raptor.common.DbOperUtil;
import org.raptor.orm.hibernate.DbaHibernateTemplateSupport;
import org.raptor.pagination.Page2;
import org.springframework.stereotype.Repository;

import com.rp.common.vo.PageParam;
import com.rp.system.po.Role;
import com.rp.system.vo.RoleVo;

@Repository
public class RoleDao extends DbaHibernateTemplateSupport<Role>{

	public static final String INSERT_ROLE = 
			"INSERT INTO RP_SYSTEM_ROLES(id,createDate,updateDate,version,name,remark,superRole) " +
			"VALUES(?,?,?,?,?,?,?)";
	
	/**
	 * 获取指定用户拥有的角色
	 * @param userId 用户ID
	 * @return
	 */
	public List<Role> getRolesByUserId(String userId){
		String hql = "from Role where users.id = ?";
		List<Role> roles = this.find(hql, userId);
		return roles;
	}
	
	/**
	 * 获取所有角色数据
	 * @return
	 */
	public List<Role> getAllRoles(){
		return this.find("from Role");
	}
	
	/**
	 * 查询角色数据，返回List列表，获取到的角色列表中已排除超级管理员角色
	 * @param roleVo
	 * @return
	 */
	public List<Role> getRoles(RoleVo roleVo){
		String hql = "from Role where name like ? and superRole = ?";
		return this.find(hql, DbOperUtil.fuzzyQueryParam(roleVo.getName()), "0");
	}
	
	/**
	 * 查询角色数据，返回List列表，获取到的角色列表中不排除超级管理员角色
	 * @param roleVo
	 * @return
	 */
	public List<Role> getRolesForSuperRole(RoleVo roleVo){
		String hql = "from Role where name like ?";
		return this.find(hql, DbOperUtil.fuzzyQueryParam(roleVo.getName()));
	}
	
	/**
	 * 分页获取角色数据
	 * @param pageParam
	 * @param roleVo
	 * @return
	 */
	public Page2 getRolesByPage(PageParam pageParam, RoleVo roleVo){
		String hql = "from Role where name like ?";
		Page2 page = this.getPage2ByOffset(hql, 
				pageParam.getPageOffset(), pageParam.getPageSize(), 
				pageParam.getSortField(), pageParam.getSortOrder(), 
				DbOperUtil.fuzzyQueryParam(roleVo.getName()));
		return page;
	}
	
}
